package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页 后台管理
 * 图表
 *
 * @author lixu
 */
@RestController
@RequestMapping("/admin/dashboard")
@CrossOrigin
public class DashboardController {

    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsCategoryService dtsCategoryService;
    @Autowired
    private DtsOrderService dtsOrderService;


    //统计的天数
    public static final Integer STATIS_DAYS_RANG = 30;//60

    /**
     * - 首页统计用户数, 商品数, 库存货品数,订单数地址 : http://localhost:8083/admin/dashboard
     * - GET请求
     * - 下行数据 :
     * - goodsTotal : 商品数量, userTotal : 用户数量 , productTotal : 货品数量 , orderTotal : 订单数量
     * <p>
     * <p>
     * {
     * "errno":0,
     * "data":{
     * "goodsTotal":426,
     * "userTotal":197,
     * "productTotal":8939,
     * "orderTotal":107
     * },
     * "errmsg":"成功"
     * }
     */
    @RequiresAuthentication
    @GetMapping
    public Object dashboard() {
        //1: 商品数量, userTotal :
        Long goodsCount = dtsGoodsService.findCount();
        // 2: 用户数量 , productTotal :
        Long usersCount = dtsUserService.findCount();
        //3: 货品数量 ,
        // 4 : 订单数量
        Map<String, Object> obj = new HashMap<String, Object>();

        obj.put("goodsTotal", goodsCount);
        obj.put("userTotal", usersCount);
        obj.put("productTotal", 8939);
        obj.put("orderTotal", 107);
        return ResponseUtil.ok(obj);
    }
    /**
     *

     - 首页数据可视化图表地址 : http://localhost:8083/admin/dashboard/chart

     - GET请求

     - 下行数据 :

     - categorySell : 商品分类  和对应分类的销售额
     {
     "errno":0,
     "data":{
     "categorySell":{
     "categoryNames":["数码家电","日用百货","鞋帽箱包"],
     "categorySellData":[
     {"name":"数码家电","value":68.76},
     {"name":"日用百货","value":48.76},
     {"name":"鞋帽箱包","value":277.53}
     ]
     },
     "orderAmts":{
     "dayData":[
     "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
     "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
     "2119-12-11","2119-12-13","2119-12-15","2119-12-16",
     "2119-12-17","2119-12-19","2119-12-20","2119-12-23",
     "2119-12-25","2119-12-27","2119-12-28","2119-12-29",
     "2119-12-30"
     ],
     "orderAmtData":[
     6.88,138.79,277.65,65.64,182.00,61.00,
     289.01,375.76,29.50,247.50,145.50,
     31.18,399.44,16.99,150.01,218.65,255.77,
     126.59,275.25,97.74,14.63
     ],
     "orderCntData":[
     1,2,4,1,3,1,2,3,1,1,16,3,9,2,1,2,2,2,2,2,2
     ]
     },
     "userOrderCnt":{
     "dayData":[
     "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
     "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
     "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
     "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
     "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
     "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
     "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
     "2119-12-29","2119-12-30"
     ],
     "userCnt":[
     5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
     4,8,8,14,3,2,1,1,2,1,2,7,3,5
     ],
     "orderCnt":[
     1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
     0,2,1,0,0,2,0,2,0,2,2,2,2
     ]
     }
     },
     "errmsg":"成功"
     }

     [] : 数组、集合
     {} ：Map、对象
     [{},{},{}]:集合<对象>


     */
    @RequiresAuthentication
    @GetMapping("/chart")
    public Object chart(){
        //1:饼图
        CategorySellVo categorySellVo = new CategorySellVo();
        List<CategorySellAmts> categorySellAmtsList = dtsCategoryService.findCategorySellVo();
        categorySellVo.setCategorySellData(categorySellAmtsList);
        categorySellVo.setCategoryNames(categorySellAmtsList
                .stream()
                .map(CategorySellAmts::getName)
                .collect(Collectors.toList())
                .toArray(new String[categorySellAmtsList.size()]));
        //2:日期和对应的订单总额, 订单笔数  26个 少了5天
        List<DayStatis> dayStatisList = dtsOrderService.findOrderDayStatisList();
        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        //日期集合
        orderAmtsVo.setDayData(dayStatisList.stream().map(DayStatis::getDayStr).
                collect(Collectors.toList()).toArray(new String[dayStatisList.size()]));
        //数量集合
        orderAmtsVo.setOrderCntData(dayStatisList.stream().map(DayStatis::getCnts).
                collect(Collectors.toList()).toArray(new Integer[dayStatisList.size()]));
        //金额集合
        orderAmtsVo.setOrderAmtData(dayStatisList.stream().map(DayStatis::getAmts).
                collect(Collectors.toList()).toArray(new BigDecimal[dayStatisList.size()]));

        //3: 日期对应的用户数（当天新增）及订单数（当前下单数)
        //3.1: 日期对应的用户数（当天新增）   长度30  少一个31号
        List<DayStatis> userDayDayNumList = dtsUserService.findUserDayNumList(STATIS_DAYS_RANG);
        //3.2://订单日期 26个 与 用户日期 30个  合并 == 31天
        String[] dayData = mergeDay(dayStatisList, userDayDayNumList);


        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        //日期天数
        userOrderCntVo.setDayData(dayData);
        //订单数
        userOrderCntVo.setOrderCnt(findStatisByDayData(dayData,dayStatisList));
        //用户数
        userOrderCntVo.setUserCnt(findStatisByDayData(dayData,userDayDayNumList));
        /**
         *   "userOrderCnt":
         *    {
         *             "dayData":
         *             [
         *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *                 "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
         *                 "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
         *                 "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
         *                 "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
         *                 "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
         *                 "2119-12-29","2119-12-30","2119-12-31"
         *             ],
         *             "userCnt":[
         *                 5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
         *                 4,8,8,14,3,2,1,1,2,1,2,7,3,5
         *             ],
         *             "orderCnt":[
         *                 1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
         *                 0,2,1,0,0,2,0,2,0,2,2,2,2
         *             ]
         *         }
         *     }
         */

        Map<String, Object> obj = new HashMap<String, Object>();
        //饼图
        obj.put("categorySell", categorySellVo);
        obj.put("orderAmts", orderAmtsVo);
        obj.put("userOrderCnt", userOrderCntVo);
        return ResponseUtil.ok(obj);

    }

    //订单日期 26个 与 用户日期 30个  合并 == 31天
    //参数1是订单日期  参数2：用户日期
    public String[]  mergeDay(List<DayStatis> dayStatisList,List<DayStatis> userDayDayNumList){
        Set<String> daySet = new HashSet<>();//去重  快 ： 无序
        //JDK1.8
        dayStatisList.stream().forEach(dayStatis -> {
            daySet.add(dayStatis.getDayStr());
        });
        userDayDayNumList.stream().forEach(dayStatis -> {
            daySet.add(dayStatis.getDayStr());
        });
        // 默认排序  从2119-12-01 开始
    /*    Collections.sort(new ArrayList<String>(daySet),(o1,o2) -> {// o1=1  o2 =5
            //从 2119-12-31 开始
            return Integer.parseInt(o2)-Integer.parseInt(o1);
        });*/
        List<String> dayList = new ArrayList<>(daySet);
        Collections.sort(dayList);
        return dayList.toArray(new String[dayList.size()]);
    }
    /**
     *  2、根据日期找订单笔数 找不到写0  同一个方法
     *
     *  3、根据日期找用户新增 找不到写0  同一个方法
     *
     *
     */
    public  Integer[] findStatisByDayData(String[] dayData,List<DayStatis> dayStatisList){
        //数量  （用户的、订单的）
        List<Integer> cntsArray = new ArrayList<>(dayData.length);


        //  2119-12-30
        for (String day : dayData) {//dayData : 31长度
            int n = 0 ;
            //标记
            //boolean flag = false;//默认视为没有
            for (DayStatis dayStatis : dayStatisList) {//dayStatisList : 30长度  2119-12-01
                if(dayStatis.getDayStr().equals(day)){
                    //表示日期是有的 、使用数量
                   // cntsArray.add(dayStatis.getCnts());
                    //相同了
                    //flag = true;//有了
                    n = dayStatis.getCnts();
                    break;//结束当前for循环
                }
            }
           cntsArray.add(n);
        }
        return cntsArray.toArray(new Integer[dayData.length]);
    }

}
